#include "stackmanagement.hpp"

StackManagement::StackManagement(QObject *parent) :
    QObject(parent)
{
    this->stackPosition = 0;
}


StackManagement::StackManagement(const StackManagement &other)
{
    this->stackPosition = other.getStackPosition();

    for (int i = 0; i < other.getStack().size(); i++)
	this->stack.push(other.getStack().at(i));
}


StackManagement::~StackManagement()
{
}


void StackManagement::reset()
{
    while(stackPosition != 0)
    {
	this->pop();
    }
}


const QStack<int> &StackManagement::getStack() const
{
    return this->stack;
}


int StackManagement::getStackPosition() const
{
    return this->stackPosition;
}


void StackManagement::push(int adress)
{
    emit pushStack(adress, this->stackPosition);

    if (this->stackPosition == 8)
    {
	this->stack.pop_front();
	qWarning() << "StackOverflow occured";
	emit stackOverflow();
    } else
	this->stackPosition++;

    this->stack.push(adress);
}


int StackManagement::pop()
{
    if (this->stack.size() == 0)
    {
	qWarning() << "Popping from empty Stack";
	return 0;
    }

    emit popStack();

    this->stackPosition--;
    return this->stack.pop();
}
